﻿using Microsoft.Live;
using Store.WP7.Framework;
using Store.WP7.Framework.Configuration;
using System;

namespace Store.WP7.ViewModels
{
    public class SettingsViewModel : PageViewModelBase
    {
        private const string FILENAME_Settings = "MyStore.Settings.txt";

        public string ClientId { get { return SkydriveService.ClientId; } }

        public string RequiredScopes { get { return SkydriveService.RequiredScopes; } }

        private bool isSkydriveConnected;
        public bool IsSkydriveConnected
        {
            get { return isSkydriveConnected; }
            set
            {
                if (isSkydriveConnected != value)
                {
                    isSkydriveConnected = value;
                    RaisePropertyChanged(() => this.IsSkydriveConnected);
                }
            }
        }

        public bool CanSendAnonymousReport
        {
            get { return ConfigurationManager.Instance.CanSendAnonymousReport; }
            set
            {
                if (ConfigurationManager.Instance.CanSendAnonymousReport != value)
                {
                    ConfigurationManager.Instance.CanSendAnonymousReport = value;
                    RaisePropertyChanged(() => this.CanSendAnonymousReport);
                }
            }
        }

        public bool CanUsePushData
        {
            get { return ConfigurationManager.Instance.CanUsePushData; }
            set
            {
                if (ConfigurationManager.Instance.CanUsePushData != value)
                {
                    ConfigurationManager.Instance.CanUsePushData = value;
                    RaisePropertyChanged(() => this.CanUsePushData);
                }
            }
        }

        public bool CanUseBackgroundAgent
        {
            get { return ConfigurationManager.Instance.CanUseBackgroundAgent; }
            set
            {
                if (ConfigurationManager.Instance.CanUseBackgroundAgent != value)
                {
                    ConfigurationManager.Instance.CanUseBackgroundAgent = value;
                    RaisePropertyChanged(() => this.CanUseBackgroundAgent);
                }
            }
        }

        private LiveConnectSession _session;

        public SettingsViewModel()
        {

        }

        public void SessionChanged(LiveConnectSession session, LiveConnectSessionStatus status)
        {
            IsSkydriveConnected = session != null && status == LiveConnectSessionStatus.Connected;

            _session = session;

            if (IsSkydriveConnected)
                SynchronizeSkydriveSettings();
        }

        private void SynchronizeSkydriveSettings()
        {
            var skydriveService = new SkydriveService(_session);

            var settings = ConfigurationManager.Instance.GetSettings();

            IsBusy = true;
            BusyMessage = "Synchronisation des paramètres en cours...";
            skydriveService.UploadFile(settings, FILENAME_Settings, () => 
            {
                IsBusy = false;
                ToastService.Instance.ShowNotification(ToastType.OK, "Synchronisation effectuée !");
            });
        }

        public void Kaboom()
        {
            // Crash !
            throw new Exception("Kaboom !");
        }
    }
}
